
import http from "../../utils/http_promise.js";

Page({

  /**
   * 页面的初始数据
   */

  data: {
    msgs:[],
    user:{},
    currentPage:1,
    isLoading:false,
    isFinished:false
  },
  
  bindFormSubmit(e){
     // console.log(e.detail.value.msg);
    let question = e.detail.value.msg;

    http.request({
       url:"/msg/save",
       method:"post",
       requireAuth:true,
       data: {question}
    })
    .then(res=>{

      if (res.data.status == 200) {

        wx.showToast({
          title: res.data.msg
        })

        setTimeout(function(){
          wx.switchTab({
            url: '/pages/msgs/msgs',
          })
        },2000)

      } else {

        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        
      }
    })
  
  },

  onReachBottom(){

    if (this.data.isLoadding || this.data.isFinished) {
      return false;
    }
    this.getData(); 

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let that = this;
    let user = http.checkLogin();
    that.setData({
      user
    })

    that.getData();
  
  },

  getData() {

    let that = this;

    //console.log(that.data.currentPage); 

    that.setData({
      isLoadding: true
    })

    http.request({
      url: "/msgs?page=" + that.data.currentPage,
      requireAuth: true
    })

    .then(res => {

      that.data.currentPage++;

      that.setData({
        isLoadding: false
      })

      let tmpArr = that.data.msgs;

      // 判断数据是否加载完成
      if (res.data.data.length == 0) {
        that.setData({
          isFinished: true
        });
      }

      tmpArr = tmpArr.concat(res.data.data);

      that.setData({
        msgs: tmpArr
      })


    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})